<?php

class Admini_ManageadminController extends Uuch_ManageController {

    public function init() {
        $this->appendCss('/css/home/warehouse.css');
        $this->appendCss('/admins/css/common.css');
        $this->appendScript('/admins/js/jquery/jquery-1.7.2.min.js')->appendScript('/js/jquery.validate.min.js')->appendScript('/js/messages_cn.js')->appendScript('/js/uuch.js');
        parent::init();
        $this->view->type = "adminuser";
    }

    public function indexAction() {
        $this->setTitle('员工账户管理');
        $pagesize = 15;
        $pageindex = $this->_getParam('start');
        if (!$pageindex) {
            $pageindex = 0;
        }
        $name = $this->_getParam("name");
        $result = Application_Model_adminuser::getFilter($name, $pagesize, $pageindex);
        $this->view->userlist = $result["list"];
        $this->view->userall = $result;
        $this->view->stype = 1;
    }

    public function userdetailAction() {
        $this->setTitle('员工账户详情');
        $id = $this->_getParam("id");
        if ($this->_getParam("operate") == "edituser") {
            $loginname = $this->_getParam("loginname");
            $loginnameItem = Application_Model_adminuser::byloginname($loginname);
            $userItem = Application_Model_adminuser::load($id);
            if ($userItem) {
                if ($loginnameItem) {
                    if ($loginnameItem->id == $id) {
                        $userItem->loginname = $loginname;
                        $userItem->nikename = $this->_getParam("nikename");
                        $userItem->password = md5($this->_getParam("password"));
                        $userItem->roleid = $this->_getParam("roleid");
                        $userItem->save();
                        Application_Model_Operate::addOperate(0, null, $userItem->loginname, "修改后台用户信息", $userItem->id);
                        $this->view->ma = 1;
                    } else {
                        $this->view->ma = 3;
                    }
                } else {
                    $userItem->loginname = $loginname;$userItem->nikename = $this->_getParam("nikename");
                    $userItem->password = md5($this->_getParam("password"));
                    $userItem->roleid = $this->_getParam("roleid");
                    $userItem->save();
                    Application_Model_Operate::addOperate(0, null, $userItem->loginname, "修改后台用户信息", $userItem->id);
                    $this->view->ma = 1;
                }
            } else {
                $this->view->ma = 2;
            }
        }
        $result = Application_Model_adminuser::load($id);
        $this->view->userdetail = $result;
        $this->view->stype = 2;
    }

    public function backlogAction()
    {
        $this->setTitle('我的待办');
        $pagesize=15;        
        $pageindex = $this->getSafeParam('start');
        if(!$pageindex){$pageindex=0;}
        $quick = $this->getSafeParam('quick');
        $starttime = $this->getSafeParam('dateinbegin');
        $endtime = $this->getSafeParam('dateinend');
        $status = $this->getSafeParam('status');
        if($status == Application_Model_Backlog::STATUS_default){
            $this->view->stype = 3; 
        } else if($status == Application_Model_Backlog::STATUS_dispose){
            $this->view->stype = 9; 
        } else if($status == Application_Model_Backlog::STATUS_processed){
            $this->view->stype = 10; 
        }
        $sourceType = $this->getSafeParam("sourceType");
        $backlogSearch = new Application_Model_Filter_BacklogSearch();
        $backlogSearch->quick = $quick;
        $backlogSearch->startTime = $starttime;
        $backlogSearch->endTime = $endtime;
        $backlogSearch->sourceType = $sourceType;
        $backlogSearch->status = $status;
        $backlogall = Application_Model_Backlog::getBacklogFilter($backlogSearch,$pagesize,$pageindex);   
        $this->view->backlogall = $backlogall;
        $backloglist = $backlogall['list'];
        $this->view->backloglist = $backloglist;
        $this->view->quick = $quick;;
        $this->view->sourceType = $sourceType;
        $this->view->status = $status;
        $this->view->dateinbegin = $starttime;
        $this->view->dateinend = $endtime;
        $this->view->sourceAll = Application_Model_Backlog::getSourceAll();
        $this->view->statusAll = Application_Model_Backlog::getStatusAll();
        
    }
    
    public function delbacklogAction()
    {
        $id = $this->_getParam('id');
        $result = Application_Model_Backlog::load($id);
        $result->delete();
        echo 1;
        exit;
    }
    
    public function backlogdetailAction()
    {
        $this->setTitle('待办详情');
        $id = $this->getSafeParam('id');
        $backlogItem = Application_Model_Backlog::load($id);
        $operate = $this->getSafeParam('operate');
        if($operate == 'editbacklog'){
            if($backlogItem){
                $status = $this->getSafeParam("status");
                $comment = str_replace("\r","",nl2br($this->_getParam('comment')));
                $solveName = $this->getSafeParam("solveName");
                $backlogItem->Status = $status;
                if($status != Application_Model_Backlog::STATUS_processed){
                    if($backlogItem->SolveName == $solveName){
                        $backlogItem->SolveName = $solveName;
                    }
                }
                $backlogItem->save();
                Application_Model_SolveLog::addSolveLog($backlogItem->Id, Application_Model_SolveLog::SolveType_Backlog, $comment);
                $this->view->ma = 1;
            }
        }
        $this->view->backlogItem = $backlogItem;
        $this->view->userAll = Application_Model_adminuser::getAll();
        $this->view->solveLog = Application_Model_SolveLog::getAllBySolveIdType($id,Application_Model_SolveLog::SolveType_Backlog);
        $this->view->stype = 3;
    }
     
    public function rebacklogAction()
    {
        $this->setTitle('添加待办事务');
        $operate = $this->_getParam('operate');
        if($operate == 'rebacklog'){
            $content = $this->getSafeParam("content");
            $solveDay = $this->getSafeParam("solveDay");
            if($content == "" || $solveDay == ""){
                $this->view->ma = 2;
            } else {
                $backlogId = Application_Model_Backlog::addBacklog($content, $this->getSafeParam("solveName"), $solveDay, $this->getSafeParam("comment"), $this->getSafeParam("sourceType"),null);
                $knowUserAll = $this->_getParam("knowUser");
                for ($i = 0; $i < count($knowUserAll); $i++) {
                    Application_Model_KnowUser::addKnowUser($backlogId, $knowUserAll[$i],  Application_Model_KnowUser::Type_Backlog);
                }
                $this->view->ma = 1;
            }
        }
        $this->view->sourceTypeAll = Application_Model_Backlog::getSourceTypeAll();
        $this->view->userAll = Application_Model_adminuser::getAll();
        $this->view->stype = 3;
    }
    
    public function knowlogAction()
    {
        $this->setTitle('我的知会');
        $pagesize=20;        
        $pageindex = $this->getSafeParam('start');
        if(!$pageindex){$pageindex=0;}
        $userId = Uuch_Util::getUserId();
        $starttime = $this->getSafeParam('dateinbegin');
        $endtime = $this->getSafeParam('dateinend');
        $knowlogItem = Application_Model_KnowUser::getKnowAll($userId,$starttime,$endtime,$pagesize,$pageindex);
        $this->view->knowlogList = $knowlogItem["list"];
        $this->view->knowlogAll = $knowlogItem;
        $this->view->dateinbegin = $starttime;
        $this->view->dateinend = $endtime;
        $this->view->stype = 4;
    }
    
    public function servicelogAction()
    {
        $this->setTitle('客服记录');
        $pagesize=15;        
        $pageindex = $this->getSafeParam('start');
        if(!$pageindex){$pageindex=0;}
        $quick = $this->getSafeParam("quick");
        $dateinbegin = $this->getSafeParam("dateinbegin");
        $dateinend = $this->getSafeParam("dateinend");
        $firstType = $this->getSafeParam("firstType");
        $functionType = $this->getSafeParam("functionType");
        $urgencyType = $this->getSafeParam("urgencyType");
        $status = $this->getSafeParam("status");
        $serviceSearch = new Application_Model_Filter_ServiceSearch();
        $serviceSearch->quick = $quick;
        $serviceSearch->firstType = $firstType;
        $serviceSearch->functionType = $functionType;
        $serviceSearch->urgencyType = $urgencyType;
        $serviceSearch->startTime = $dateinbegin;
        $serviceSearch->endTime = $dateinend;
        $serviceSearch->status = $status;
        $result = Application_Model_Service::getserFilter($serviceSearch,$pagesize, $pageindex);
        $this->view->servicelist = $result["list"];
        $this->view->serviceall = $result;
        $this->view->quick = $quick;
        $this->view->dateinbegin = $dateinbegin;
        $this->view->dateinend = $dateinend;
        $this->view->firstType = $firstType;
        $this->view->functionType = $functionType;
        $this->view->urgencyType = $urgencyType;
        $this->view->status = $status;
        $this->view->firstTypeAll = Application_Model_Service::getFirstTypeAll();
        $this->view->functionTypeAll = Application_Model_Service::getFunctionTypeAll();
        $this->view->urgencyTypeAll = Application_Model_Service::getUrgencyTypeAll();
        $this->view->statusAll = Application_Model_Service::getStatusAll();
        $this->view->stype = 5;
    }
    
    public function addserlogAction()
    {
        $operate = $this->getSafeParam('operate');
        $this->setTitle('添加客服记录');
        if($operate == 'addser'){
            $qq = $this->getSafeParam('qq');
            $serName = $this->getSafeParam('serName');
            $content = str_replace("\r","",nl2br($this->_getParam('content')));
            $userName = $this->getSafeParam("userName")?$this->getSafeParam("userName"):"匿名";
            $storeNo = $this->getSafeParam("storeNo")?$this->getSafeParam("storeNo"):"暂无";
            $cityName = $this->getSafeParam("cityName");
            if($qq == "" || $serName == "" || $content == "" || $cityName == ""){
                $this->view->ma = 2;
            }else{
                $serviceItem = new Application_Model_Service();
                $serviceItem->UserName = $userName;
                $serviceItem->DateCreated = date("Y-m-d H:i:s");
                $serviceItem->StoreNo = strtoupper($storeNo);
                $serviceItem->Qq = $this->getSafeParam("qq");
                $serviceItem->State = $cityName;
                $serviceItem->SerName = $serName;
                $serviceItem->Content = $content;
                $serviceItem->FirstType = $this->getSafeParam("firstType");
                $serviceItem->FunctionType = $this->getSafeParam("functionType");
                $serviceItem->UrgencyType = $this->getSafeParam("urgencyType");
                $serviceItem->Status = $this->getSafeParam("status");
                $serviceItem->UserType = $this->getSafeParam("userType");
                $serviceItem->Operator = Uuch_Util::getUserNikeName();
                $serviceItem->save();
                if($serviceItem->Status == Application_Model_Service::Status_WaitDispose){
                    $backlogId = Application_Model_Backlog::addBacklog($content, $this->getSafeParam("solveName"), $this->getSafeParam("solveDay"), $this->getSafeParam("comment"), Application_Model_Backlog::Source_Service,$serviceItem->Id);
                    $knowUserAll = $this->_getParam("knowUser");
                    for ($i = 0; $i < count($knowUserAll); $i++) {
                        Application_Model_KnowUser::addKnowUser($backlogId, $knowUserAll[$i],  Application_Model_KnowUser::Type_Service);
                    }
                }
                $this->view->ma = 1;
            }
        }
        $words = array();
        for($i=0;$i<26;$i++){
            $words[]= chr(65+$i);
        }
        $this->view->words = $words;
        $this->view->firstTypeAll = Application_Model_Service::getFirstTypeAll();
        $this->view->functionTypeAll = Application_Model_Service::getFunctionTypeAll();
        $this->view->urgencyTypeAll = Application_Model_Service::getUrgencyTypeAll();
        $this->view->userTypeAll = Application_Model_Service::getUserTypeAll();
        $this->view->userAll = Application_Model_adminuser::getAll();
        $this->view->stype = 5;
    }
    
    public function servicedetailAction()
    {
        $this->setTitle('客服记录详情');
        $id = $this->getSafeParam("id");
        $result = Application_Model_Service::load($id);
        $operate = $this->getSafeParam('operate');
        if($operate == 'editserlog'){
            if($result){
                $qq = $this->getSafeParam('qq');
                $serName = $this->getSafeParam('serName');
                $content = str_replace("\r","",nl2br($this->_getParam('content')));
                $userName = $this->getSafeParam("userName")?$this->getSafeParam("userName"):"匿名";
                $storeNo = $this->getSafeParam("storeNo")?$this->getSafeParam("storeNo"):"暂无";
                $cityName = $this->getSafeParam("cityName");
                if($qq == "" || $serName == "" || $content == "" || $cityName == ""){
                    $this->view->ma = 2;
                }else{
                    $result->UserName = $userName;
                    $result->DateCreated = date("Y-m-d H:i:s");
                    $result->StoreNo = $storeNo;
                    $result->Qq = $this->getSafeParam("qq");
                    $result->State = $cityName;
                    $result->SerName = $serName;
                    $result->Content = $content;
                    $result->FirstType = $this->getSafeParam("firstType");
                    $result->FunctionType = $this->getSafeParam("functionType");
                    $result->UrgencyType = $this->getSafeParam("urgencyType");
                    $result->Status = $this->getSafeParam("status");
                    $result->UserType = $this->getSafeParam("userType");
                    $result->Operator = Uuch_Util::getUserNikeName();
                    $result->save();
                    $this->view->ma = 1;
                }
            }else{
                $this->view->ma = 3;
            }
        } 
        $this->view->serviceItem = $result;
        $words = array();
        for($i=0;$i<26;$i++){
            $words[]= chr(65+$i);
        }
        $this->view->words = $words;
        $this->view->firstTypeAll = Application_Model_Service::getFirstTypeAll();
        $this->view->functionTypeAll = Application_Model_Service::getFunctionTypeAll();
        $this->view->urgencyTypeAll = Application_Model_Service::getUrgencyTypeAll();
        $this->view->userTypeAll = Application_Model_Service::getUserTypeAll();
        $this->view->userAll = Application_Model_adminuser::getAll();
        $this->view->stype = 5;
    }
    
    public function mylaunchAction()
    {
        $this->setTitle('我的发起');
        $pagesize=20;        
        $pageindex = $this->getSafeParam('start');
        if(!$pageindex){$pageindex=0;}
        $userName = Uuch_Util::getUserNikeName();
        $starttime = $this->getSafeParam('dateinbegin');
        $endtime = $this->getSafeParam('dateinend');
        $backlogSearch = new Application_Model_Filter_BacklogSearch();
        $backlogSearch->startTime = $starttime;
        $backlogSearch->endTime = $endtime;
        $backlogSearch->operator = $userName;
        $backlogItem = Application_Model_Backlog::getBacklogFilter($backlogSearch, $pagesize, $pageindex);
        $this->view->backloglist = $backlogItem["list"];
        $this->view->backlogAll = $backlogItem;
        $this->view->dateinbegin = $starttime;
        $this->view->dateinend = $endtime;
        $this->view->stype = 7;
    }

    public function delserlogAction()
    {
        $id = $this->_getParam('id');
        $result = Application_Model_Service::load($id);
        $result->delete();
        echo 1;
        exit;
    }
    
    public function adduserAction() {
        $this->setTitle('添加员工');
        if ($this->_getParam("operate") == "addadminuser") {
            $loginname = $this->_getParam("loginname");
            $loginnameItem = Application_Model_adminuser::byloginname($loginname);
            if ($loginnameItem) {
                $this->view->ma = 2;
            } else {
                $userItem = new Application_Model_adminuser();
                $userItem->loginname = $loginname;
                $userItem->nikename = $this->_getParam("nikename");
                $userItem->password = md5($this->_getParam("password"));
                $userItem->roleid = $this->_getParam("roleid");
                $userItem->loginnum = 0;
                $userItem->save();
                Application_Model_Operate::addOperate(0, null, $userItem->loginname, "添加后台用户信息", $userItem->id);
                $this->view->ma = 1;
            }
        }
        $this->view->stype = 1;
    }

    public function deluserAction() {
        $idlist = $this->_getParam("idlist");
        $arrid = explode(",", $idlist);
        for ($i = 0; $i < count($arrid); $i++) {
            $userItem = Application_Model_adminuser::load($arrid[$i]);
            if ($userItem) {
                Application_Model_Operate::addOperate(0, null, $userItem->loginname, "删除后台用户信息", $userItem->id);
                $userItem->delete();
            }
        }
        echo 1;
        exit;
    }

    public function byloginnameAction() {
        $id = $this->_getParam("id");
        $loginname = $this->_getParam("loginname");
        $loginItem = Application_Model_adminuser::byloginname($loginname);
        if ($loginItem) {
            if ($id == "") {
                echo 1;
            } else {
                if ($loginItem->id == $id) {
                    echo 2;
                } else {
                    echo 1;
                }
            }
        } else {
            echo 2;
        }
        exit;
    }

    public function taobaoapiAction() {
        $this->setTitle('淘宝API');
        $pagesize = 15;
        $pageindex = $this->_getParam('start');
        if (!$pageindex) {
            $pageindex = 0;
        }
        $name = $this->_getParam("name");
        $operate = $this->_getParam("operate");
        if ($operate == "edittaobao") {
            $id = $this->_getParam("id");
            $taobaoItem = Application_Model_Taobaoapi::load($id);
            if ($taobaoItem) {
                $taobaoItem->api = $this->_getParam("api");
                $taobaoItem->sessionkey = $this->_getParam("sessionkey");
                $taobaoItem->freshtoken = $this->_getParam("freshtoken");
                $taobaoItem->save();
                Application_Model_Operate::addOperate(0, null, $taobaoItem->id, "修改淘宝API信息", 0);
                $this->view->ma = 1;
            } else {
                $this->view->ma = 2;
            }
        }
        if ($operate == "addtaobao") {
            $taobaoname = Application_Model_Taobaoapi::bytaobaoneme($this->_getParam("taobaoname"));
            if ($taobaoname) {
                $this->view->ma = 4;
            } else {
                $taobaoItem = new Application_Model_Taobaoapi();
                $taobaoItem->taobaoname = $this->_getParam("taobaoname");
                $taobaoItem->api = $this->_getParam("api");
                $taobaoItem->sessionkey = $this->_getParam("sessionkey");
                $taobaoItem->freshtoken = $this->_getParam("freshtoken");
                $taobaoid = substr($taobaoItem->freshtoken, -4);
                $taobaoItem->taobaoSign = substr($taobaoid, -2).substr($taobaoid, 0, 2);
                $taobaoItem->save();
                Application_Model_Operate::addOperate(0, null, $taobaoItem->id, "添加淘宝API信息", 0);
                $this->view->ma = 3;
            }
        }
        $result = Application_Model_Taobaoapi::getFilter($name, $pagesize, $pageindex);
        $this->view->taobaolist = $result["list"];
        $this->view->taobaoall = $result;
        $this->view->name = $name;
        $this->view->stype = 6;
    }

    public function deltaobaoAction() {
        $idlist = $this->_getParam("idlist");
        $arrid = explode(",", $idlist);
        for ($i = 0; $i < count($arrid); $i++) {
            $taobaoItem = Application_Model_Taobaoapi::load($arrid[$i]);
            if ($taobaoItem) {
                Application_Model_Operate::addOperate(0, null, $taobaoItem->id, "删除淘宝API信息", 0);
                $taobaoItem->delete();
            }
        }
        echo 1;
        exit;
    }

    public function bytaobaoAction() {
        $id = $this->_getParam("id");
        $taobaoItem = Application_Model_Taobaoapi::load($id);
        if ($taobaoItem) {
            $this->json(array('type' => 1, 'taobaoname' => $taobaoItem->taobaoname, 'api' => $taobaoItem->api, 'sessionkey' => $taobaoItem->sessionkey, 'freshtoken' => $taobaoItem->freshtoken));
        } else {
            $this->json(array('type' => 2));
        }
    }
    

    public function kingtransAction() {
        $country = $this->_getParam("country");
        $agentList = Application_Model_AgentCompany::getAgentAll();
        $this->view->agentCompanyList = $agentList;
        if (strlen($country) > 0) {
            $goodstype = $this->_getParam("goodstype");
            $startWeight = $this->_getParam("startWeight");
            $endWeight = $this->_getParam("endWeight");
            $agentId = $this->_getParam("agentCompany");

            $kingTransPrice = new Application_Model_ConvertKingTrans();
            $weight = $startWeight;
//            $country = array("US", "GB");
//            foreach ($country as $value) { 
                $kingTransPrice->deleteAll($country, $startWeight, $endWeight);
                try {
                    while ($weight <= $endWeight) {
                        $kingTransPrice->getSitePrice($agentId, $country, $weight, $goodstype);
                        $weight+=0.5;
                    }
                    $this->view->startW = $weight;
                    $this->view->endW = $weight + 2 - 0.5;
                    $this->view->country = $this->_getParam("country");
                } catch (Exception $e) {
                    print_r("请使用" . $weight . "作为下次的起初重量重新获取" . "<br />");
                    print $e->getMessage();
                }
//            }

            $this->view->result = "获取到数据,请查询数据库";
        }
    }
    public function userinfoAction()
    {
        $storeno = $this->getSafeParam("storeno");
        $userItem = Application_Model_User::bystore($storeno);
        if($userItem){
            $billCount = Application_Model_wayBillItem::getCountByUserId($userItem->Id);
            $userType = $billCount==0?(Application_Model_Service::UserType_New):(Application_Model_Service::UserType_Old);
            $this->json(array("type"=>1,"usreName"=>$userItem->Nikename,"userType"=>$userType,"billCount"=>$billCount));
        } else {
            $this->json(array("type"=>0));
        }
    }
    
    public function analyze4pxAction()
    {
        $this->setTitle('解析4PX');
        $agentMessage = "<h1>转单号解析结果如下：</h1><br>";
        $moneyMessage = "<h1>成本价解析结果如下：</h1><br>";
        if($this->isPost()){
            if($_FILES["agentFile"]["type"] == "text/csv"){
                $str=fopen($_FILES['agentFile']['tmp_name'],"r");
                $row = 1;
                while ($data = fgetcsv($str, 1000, ",")) {
//                    $num = count($data);
                    if($row>1){
//                        for ($c=0; $c < $num; $c++) {
//                            $csv_arr[$row][$c]=$data[$c];
//                        }
                        $billItem = Application_Model_wayBillItem::byAgentNo($data[0]);
                        if(!$billItem){
                            $billItem = Application_Model_wayBillItem::byAgentNo($data[1]);
                        }
                        if(!$billItem){
                            $agentMessage .= "<font color='#c00;'>订单为：".$data[0]."或者".$data[1]."的订单查询不到，请运单专员检查。</font><br>";
                            continue;
                        }
                        $billItem->LastWeight = $data[2];
                        if(count(explode("YQGJSX", $data[1]))==1){
                            $billItem->Logistics = $data[1];
                        }
                        $billItem->save();
                        $agentMessage .= "<font style='margin-left:22px;'>订单为：".$data[0]."或者".$data[1]."的订单重量和转单号已更新。</font><br>";
                    }
                    $row++;
                }
            } else {
                $this->view->ma = 2;
            }
            if($_FILES["moneyFile"]["type"] == "text/csv"){
                $str=fopen($_FILES['moneyFile']['tmp_name'],"r");
                $row = 1;
                while ($data = fgetcsv($str, 1000, ",")) {
                    if($row>1){
                        $billItem = Application_Model_wayBillItem::byAgentNo($data[0]);
                        if(!$billItem){
                            $moneyMessage .= "<font color='#c00;' style='margin-left:22px;'>订单为：".$data[0]."的订单查询不到，请运单专员检查。</font><br>";
                            continue;
                        }
                        $billItem->CostMoney = $data[13];
                        $billItem->save();
                        $moneyMessage .= "<font style='margin-left:22px;'>订单为：".$data[0]."的订单成本价已更新。</font><br>";
                    }
                    $row++;
                }
            } else {
                $this->view->ma = 2;
            }
        }
        $this->view->agentMessage = $agentMessage;
        $this->view->moneyMessage = $moneyMessage;
        $this->view->stype = 7;
    }
}